/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inicio;

import MenuPrincipal.MenuPrincipalController;
import dataBase.Db;
import hate.EntradaSalida;
import java.awt.GridLayout;
import java.awt.event.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import usuarios.Vendedor;
import usuarios.crearVendedor;

/**
 *
 * @author martinLequerica
 */
public class arranca {

    private String[] labels = {"Nombre de Usuario", "Contraseña"};
    private JTextField usuario;
    
    private JPasswordField contrasenia;
    private JButton aceptar, cancelar;
    private JFrame jf;
    private JPanel jp;

    public arranca() throws SQLException {


        Db dbf = new Db();
        Statement st1 = dbf.getStatement();
        
        ResultSet eq = st1.executeQuery("select * from vendedor");
        
        if (!eq.next()){
            //el no es de que no esta iniciado el programa
            new crearVendedor("no");
            
        }else{
        
        

        
        
        
        jf = new JFrame();
        jp = new JPanel();
        jp.setLayout(new GridLayout(3, 2));
        jf.add(jp);
        jf.setDefaultCloseOperation(3);

        jf.setSize(360, 105);
        //inicializo
        usuario = new JTextField("fotocopiadora");
        contrasenia = new JPasswordField("fotocopiadora");
        

        aceptar = new JButton("Aceptar");
        cancelar = new JButton("Cancelar");

        jp.add(new JLabel(labels[0]));
        jp.add(usuario);
        jp.add(new JLabel(labels[1]));
        jp.add(contrasenia);
        jp.add(aceptar);
        jp.add(cancelar);

        
        contrasenia.addKeyListener(new KeyListener() {

                @Override
                public void keyTyped(KeyEvent ke) {
                    
                }

                @Override
                public void keyPressed(KeyEvent ke) {
                    if (ke.getKeyCode() == 10){
                        iniciarSesion();
                    }
                }

                @Override
                public void keyReleased(KeyEvent ke) {
                    
                }
            });

        aceptar.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {

               iniciarSesion();



            }

                
        });
        
        
        cancelar.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
           
                System.exit(0);
                
            }
        });

        contrasenia.requestFocusInWindow();
        contrasenia.requestFocus();
        jf.setVisible(true);


        }
    }
    
    public void iniciarSesion(){
         Db db = new Db();
                Statement st = db.getStatement();
                char[] password = contrasenia.getPassword();
                String pas = "";
                for (int i = 0; i < password.length; i++) {

                    pas += password[i];

                }




                String sql = "select * from vendedor where nombreUsuario = '" + usuario.getText() + "' && contrasenia = '" + new EntradaSalida().hash(pas) + "'";
                try {
                    ResultSet eq = st.executeQuery(sql);

                    if (eq.next()) {
                        jf.setVisible(false);
                        String idVendedor = eq.getString("idvendedor");
                        new MenuPrincipalController(new Vendedor(eq));

                    }else {
                        JOptionPane.showMessageDialog(null, "Usuario o contrasenia incorrecto, no moleste al administrador.");
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(arranca.class.getName()).log(Level.SEVERE, null, ex);
                }

    }
}
